﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.Utility;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;

namespace EHTWebsiteProject
{
    public partial class OrderDetails : System.Web.UI.Page
    {
        private static int OrderID = -1;
        private static int OrderState = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["oid"] is object)
                {
                    OrderID = Int32.Parse(Request.QueryString["oid"]);
                    Session["OrderID"] = OrderID.ToString();
                    OrderState = GetStateOrder(OrderID);
                    Session["OrderState"] = OrderState.ToString();
                    LoadRegion();
                    if (OrderState == 1)
                        LoadOrderID(OrderID);
                    else
                        if (OrderState == 2)
                        {
                            btnSave.Visible = false;
                            btnReturn.Visible = false;
                            LoadOrderID(OrderID);
                            ltrError.Text = "Order shipping.Can't Update or Return";
                        }
                        else
                        {
                            btnReturn.Visible = false;
                            btnSave.Visible = false;
                            LoadOrderID(OrderID);
                            ltrError.Text = "Order shipped.Can't Update or Return";
                        }
                }
            }
        }

        private void LoadOrderID(Int32 OrderID)
        {
            try
            {
                Order theOrder = OrderProvider.GetOrderByOrderID(OrderID);
                txtPostCode.Text = theOrder.ReceivedPostalCode.ToString();
                txtReceicedPhone.Text = theOrder.ReceicedPhone.ToString();
                txtReceivedAddress.Text = theOrder.ReceivedAddress.ToString();
                txtReceivedCity.Text = theOrder.ReceivedCity.ToString();
                txtReceivedName.Text = theOrder.ReceivedName.ToString();
                try
                {
                    ddlReceivedRegion.SelectedValue = theOrder.ReceivedRegionID.ToString();
                }
                catch
                {
                    ddlReceivedRegion.SelectedValue = "-1";
                }
                cdRequredDate1.SelectedDate = theOrder.RequireDate;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void LoadRegion()
        {
            try
            {
                ddlReceivedRegion.DataSource = RegionProvider.GetRegions();
                ddlReceivedRegion.DataTextField = "RegionName";
                ddlReceivedRegion.DataValueField = "RegionID";
                ddlReceivedRegion.DataBind();
                ddlReceivedRegion.Items.Insert(0, new ListItem("Select regions", "-1"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateDateTime())
                return;
            Page.Validate();
            if (Page.IsValid)
            {
                MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                Order theOrder = new Order();
                theOrder.BillPictureUrl = "";
                theOrder.CustomerID = theUser.ProviderUserKey.ToString();
                theOrder.OrderID = Int32.Parse(Session["OrderID"].ToString());
                theOrder.OrderState = HangSo.ORDER_STATE_APPENDING;
                theOrder.ReceicedPhone = this.txtReceicedPhone.Text.Trim();
                theOrder.ReceivedAddress = this.txtReceivedAddress.Text.Trim();
                theOrder.ReceivedCity = this.txtReceivedCity.Text.Trim();
                theOrder.ReceivedName = this.txtReceivedName.Text.Trim();
                theOrder.ReceivedPostalCode = this.txtPostCode.Text.Trim();
                theOrder.ReceivedRegionID = Int32.Parse(this.ddlReceivedRegion.SelectedValue.ToString());
                theOrder.RequireDate = cdRequredDate1.SelectedDate.Value.AddHours(DateTime.Now.Hour + 5);
                theOrder.ServiceCost = RegionProvider.GetRegionByRegionID(Int32.Parse(this.ddlReceivedRegion.SelectedValue.ToString())).ServiceCost;
                theOrder.TotalAmount = ShoppingCart.Instance.GetSubTotal();

                OrderProvider.UIOrder(theOrder, false);

                Response.Redirect("HistoriePalegg-" + Session["OrderState"].ToString() + ".html");
            }
            
        }

        private Boolean ValidateDateTime()
        {
            string msg = "";
            Boolean isSuccess = true;
            if (ddlReceivedRegion.SelectedValue == "-1")
            {
                msg = "Select a Region";
                isSuccess = false;
            }
            DateTime RequredDate;
            DateTime NowDate = new DateTime();
            RequredDate = cdRequredDate1.SelectedDate.Value;
            if (RequredDate == NowDate)
            {
                rDate.IsValid = false;
                isSuccess = false;
            }
            if (RequredDate.Date < DateTime.Now.Date)
            {
                rDate.ErrorMessage = "Required date must be equal or greater than current date.";
                rDate.IsValid = false;
                isSuccess = false;
            }
            if (!isSuccess)
            {
                ltrMessage.Text = msg;
            }
            return isSuccess;
        }

     
        public static int GetStateOrder(Int32 OrderID)
        {
            Int32 intState;
            Order theOrder = OrderProvider.GetOrderByOrderID(OrderID);
            intState = theOrder.OrderState;
            return intState;
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            // xoa order details co orderID = order ID hien tai
            OrderDetaiProvider.DeleteOrderDetailOderByOrderID(Int32.Parse(Session["OrderID"].ToString()));
            // xoa order order ID hien tai
            OrderProvider.DeleteOderByOrderID(Int32.Parse(Session["OrderID"].ToString()));
            Response.Redirect("HistoriePalegg-" + Session["OrderState"].ToString() + ".html");
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("HistoriePalegg-" + Session["OrderState"].ToString() + ".html");
        }
    }
}